#include "ringQueue.hpp"
#include <unistd.h>
#include <ctime>
#include<sys/types.h>
#include<unistd.h>



void *consumer(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;

    while (true)
    {
        sleep(1);

        int x;
        //1. 从环形队列中取数据
        rq->pop(&x);
        //2.进行一定的处理
        std::cout<<" 消费： "<< x <<" ["<<pthread_self()<<" ]"<<std::endl;
    }
    
}


void *productor(void *args)
{
    RingQueue<int> *rq = (RingQueue<int> *)args;

    while (true)
    {
        //sleep(1);
        //1.构建数据或者对象
        int x = rand()%100+1;
        std::cout<<" 生产： "<<x<<" ["<<pthread_self()<<"] "<<std::endl;

        //2.推送到环形队列中
        rq->push(x);
    }
    
}



int main()
{
    srand((uint64_t)time(nullptr)^getpid());
    RingQueue<int> *rq = new RingQueue<int>();

    pthread_t c[3],p[2];
    pthread_create(c, nullptr, consumer, (void *)rq);
    pthread_create(c+1, nullptr, consumer, (void *)rq);
    pthread_create(c+2, nullptr, consumer, (void *)rq);

    pthread_create(p, nullptr, productor, (void *)rq);
    pthread_create(p+1, nullptr, productor, (void *)rq);

    for(int i = 0; i<3;i++) pthread_join(c[i], nullptr);
    for(int i = 0;i<2;i++) pthread_join(p[i], nullptr);

    return 0;

}